home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / FIX_DESK / SOURCE / FIX.C < prev    next >
C/C++ Source or Header  |  1988-09-07  |  5KB  |  239 lines

  1. #define DEBUG_MODE
  2.  
  3. /************************************************************/
  4. /* Program Fix Desktop: clean up the desktop file.          */
  5. /*   Removes extraneous information from the desktop.       */
  6. /*                                                          */
  7. /*   Version 1.0 [ 7/ 9/87]: Original implementation.       */
  8. /*           1.1 [ 7/16/87]: Support for MFS volumes added. */
  9. /*             2.0 [ 8/22/88]: Converted to Lightspeed C.        */
  10. /*                                                          */
  11. /************************************************************/
  12.  
  13. #include <MacTypes.h>
  14. #include <QuickDraw.h>
  15. #include <MenuMgr.h>
  16. #include <WindowMgr.h>
  17. #include <DialogMgr.h>
  18. #include <EventMgr.h>
  19. #include <FileMgr.h>
  20. #include <HFS.h>
  21.  
  22. #define FIX_MAIN
  23. #include "fix.h"
  24.  
  25. static MenuHandle menulist[4];
  26.  
  27. void main(void);
  28. void check_hfs(void);
  29. void initialize_toolbox(void);
  30. void initialize_menus(void);
  31. void handle_events(void);
  32. int menu_command(long menu_choice, int was_key);
  33. void apple_command(int what);
  34. int file_command(int what);
  35.  
  36.     /* Main program. */
  37.  
  38. void main()
  39. {
  40.     check_hfs();
  41.  
  42.     MaxApplZone();
  43.     initialize_toolbox();
  44.     initialize_menus();
  45.  
  46.     handle_events();
  47.  
  48.     InitWindows();
  49.     set_watch_cursor();
  50. }
  51.  
  52.     /* Check that the Hierarchical File System is available. */
  53.  
  54. static void check_hfs()
  55. {
  56.     if (FSFCBLen < 0) {                /* Not HFS */
  57.         StopAlert(ALERT_ROM,NIL);
  58.         ExitToShell();
  59.     }
  60. }
  61.  
  62.     /* Initialize all ToolBox routines used by the program. */
  63.  
  64. #ifdef DEBUG_MODE
  65. void goodbye(void);
  66.  
  67. static void goodbye()
  68. {
  69.     ExitToShell();
  70. }
  71. #endif
  72.  
  73. static void initialize_toolbox()
  74. {
  75.     InitGraf(&thePort);
  76.     InitFonts();
  77.     InitWindows();
  78.     InitMenus();
  79.     TEInit();
  80.  
  81. #ifdef DEBUG_MODE
  82.     InitDialogs(goodbye);
  83. #else
  84.     InitDialogs(NIL);
  85. #endif
  86.  
  87.     FlushEvents(everyEvent, 0);
  88.     InitCursor();
  89. }
  90.  
  91.     /* Load our menus and draw the menu bar. */
  92.  
  93. static void initialize_menus()
  94. {
  95.     register Handle my_menus;    /* Menu bar... */
  96.  
  97.     my_menus = GetNewMBar(MBAR_RESOURCE);
  98.     SetMenuBar(my_menus);
  99.     DisposHandle(my_menus);
  100.  
  101.     menulist[APPLE_MENU]    = GetMHandle(APPLE_MENU);
  102.     menulist[FILE_MENU]     = GetMHandle(FILE_MENU);
  103.     menulist[EDIT_MENU]     = GetMHandle(EDIT_MENU);
  104.  
  105.     AddResMenu(menulist[APPLE_MENU], 'DRVR');
  106.  
  107.     DisableItem(menulist[FILE_MENU], FILE_CLOSE);    /* Disable  */
  108.     DisableItem(menulist[EDIT_MENU], 0);    /* some commands... */
  109.  
  110.     DrawMenuBar();
  111. }
  112.  
  113.     /* Main event loop. */
  114.  
  115. static void handle_events()
  116. {
  117.     EventRecord myevent;
  118.     register int quit_flag, desk_status, new_desk_status;
  119.  
  120.     quit_flag = 0;
  121.     desk_status = 0; /* No desk accessory is initially open. */
  122.     while (!quit_flag) {
  123.         new_desk_status = (FrontWindow() != NIL);
  124.         if (new_desk_status != desk_status) {    /* Update menus. */
  125.             desk_status = new_desk_status;
  126.             if (desk_status) {
  127.                 EnableItem(menulist[FILE_MENU], FILE_CLOSE);
  128.                 EnableItem(menulist[EDIT_MENU], 0);
  129.                 DrawMenuBar();
  130.             }
  131.             else {
  132.                 DisableItem(menulist[FILE_MENU], FILE_CLOSE);
  133.                 DisableItem(menulist[EDIT_MENU], 0);
  134.                 DrawMenuBar();
  135.             }
  136.         }
  137.         SystemTask();
  138.         GetNextEvent(everyEvent, &myevent);
  139.         switch(myevent.what) {
  140.             case mouseDown:
  141.                 {
  142.                     WindowPtr which_window;
  143.  
  144.                     switch(FindWindow(myevent.where, &which_window)) {
  145.                         case inSysWindow:
  146.                             SystemClick(&myevent, which_window);
  147.                             break;
  148.                         case inMenuBar:
  149.                             quit_flag = menu_command(MenuSelect(myevent.where), 0); /* not keypress */
  150.                             break;
  151.                     }
  152.                 }
  153.                 break;
  154.             case keyDown:
  155.             case autoKey:
  156.                 if (myevent.modifiers & cmdKey)
  157.                     quit_flag = menu_command(MenuKey((char)myevent.message), 1); /* was keypress */
  158.                 break;
  159.         }
  160.     }
  161. }
  162.  
  163.     /* Dispatch a menu selection.  Called from the event loop. */
  164.  
  165. static int menu_command(menu_choice, was_key)
  166.     long menu_choice;
  167.     int was_key;
  168. {
  169.     register int which_menu, which_item;
  170.  
  171.     which_menu = (int)(menu_choice>>16);
  172.     which_item = (int)menu_choice;
  173.  
  174.     HiliteMenu(which_menu);
  175.     switch(which_menu) {
  176.         case APPLE_MENU:
  177.             apple_command(which_item);
  178.             break;
  179.         case FILE_MENU:
  180.             if (file_command(which_item)) {
  181.                 HiliteMenu(0);
  182.                 return(1);
  183.             }
  184.             break;
  185.         case EDIT_MENU:
  186.             if (!was_key)
  187.                 SystemEdit(which_item-1);
  188.             break;
  189.     }
  190.     HiliteMenu(0);
  191.     return(0); /* Not ready to quit yet... */
  192. }
  193.  
  194.     /* Handle an Apple menu selection. */
  195.  
  196. static void apple_command(what)
  197.     int what;
  198. {
  199.     register DialogPtr about_dialog;
  200.     int temp;
  201.  
  202.     if (what == APPLE_ABOUT) {
  203.         about_dialog = GetNewDialog(DIALOG_ABOUT, NIL, MINUS_ONE);
  204.         ModalDialog(NIL, &temp);
  205.         DisposDialog(about_dialog);
  206.     }
  207.     else {
  208.         GetItem(menulist[APPLE_MENU], what, string);
  209.         OpenDeskAcc(string);
  210.     }
  211. }
  212.  
  213.     /* Handle a file menu selection. */
  214.  
  215. static int file_command(what)
  216.     int what;
  217. {
  218.     switch(what) {
  219.         case FILE_OPTIONS:
  220.             do_options();
  221.             break;
  222.         case FILE_PROCESS:
  223.             one_pass();
  224.             break;
  225.         case FILE_CLOSE:
  226.             {
  227.                 register int refnum;
  228.  
  229.                 if ((refnum = ((WindowPeek) FrontWindow())->windowKind) < 0)
  230.                     CloseDeskAcc(refnum);
  231.             }
  232.             break;
  233.         case FILE_QUIT:
  234.             return(1);    /* We're quitting! */
  235.             break;
  236.     }
  237.     return(0);    /* Not a quit... */
  238. }
  239.